package cn.edu.ctbu.lab.api;

import cn.edu.ctbu.lab.domain.Teacher;
import cn.edu.ctbu.lab.helper.RUtil;
import cn.edu.ctbu.lab.service.TeacherService;
import cn.edu.ctbu.lab.vo.BTPageDO;
import cn.edu.ctbu.lab.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
public class TeacherRestController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 基本分页
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(value = "/api/teacher/list")
    public BTPageDO<Teacher> list(int offset, int limit){

        int pageIndex=offset/limit;
        int pageSize=limit;

        Page<Teacher> myList=teacherService.findByPage(pageIndex,pageSize);

        BTPageDO<Teacher> stuList=new BTPageDO<Teacher>();
        stuList.setLimit(pageSize);
        stuList.setOffset(pageIndex);
        stuList.setRows(myList.getContent());
        Long total=myList.getTotalElements();
        stuList.setTotal(total.intValue());

        return  stuList;


    }


    @GetMapping("/api/teacher/read")
    public R read(Integer id){
        Teacher teacher= teacherService.findById(id).get();

        return RUtil.success(teacher);

    }


    @PostMapping(value = "/api/teacher/add")
    public R add(@Valid Teacher teacher, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return RUtil.error(-2,
                    "出错了!"+bindingResult.getFieldError().getDefaultMessage());
        }
        teacherService.add(teacher);

        return RUtil.success();

    }


    @PutMapping(value = "/api/teacher/update")
    public R update(@Valid Teacher teacher, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return RUtil.error(-2,
                    "出错了!"+bindingResult.getFieldError().getDefaultMessage());
        }
        String newPassword=teacher.getPassword();
        if(newPassword.equals("")){
            //没有 改密码
            Teacher oldteacher = teacherService.findById(teacher.getId()).get();
            teacher.setPassword(oldteacher.getPassword());
            //需要使用原来的密码
        }

        teacherService.update(teacher);

        return RUtil.success();

    }


    @DeleteMapping(value = "/api/teacher/delete")
    public R delete(Integer[] ids){

        Integer mylen= ids.length;
        for (int i = 0; i <mylen ; i++) {

            //读取数据,再删除
            //Teacher item = teacherService.findById(ids[i]).get();

            teacherService.delete(ids[i]);


        }

        return RUtil.success();

    }


    @GetMapping(value = "/api/teacher/query")
    public R query(String q){


        List<Teacher> teacherList= teacherService.findByNameAndPage(q);


        return RUtil.success(teacherList);
    }


}
